package com.hundsun.cprs.yyt.web.action.account;

import com.hundsun.cprs.yyt.service.account.domain.Account;
import com.hundsun.cprs.yyt.service.account.dto.AccountVo;
import com.hundsun.cprs.yyt.util.ExportExcelUtil;
import com.hundsun.cprs.yyt.web.util.ExportExcelWrapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hundsun.cprs.yyt.service.account.domain.query.AccountQuery;
import com.hundsun.cprs.yyt.service.account.service.AccountService;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import com.hundsun.network.eclp.ex.common.PermissionEnum;
import com.hundsun.network.eclp.ex.security.AdminAccess;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountAction extends BaseAction {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/list")
    @AdminAccess(PermissionEnum.ACCOUNT_MANAGE_LIST)
    public void list(@ModelAttribute("query") AccountQuery query,ModelMap model) {
        accountService.selectByPage(query);
        
//        AccountQuery sumQuery = new AccountQuery();
//        BeanUtils.copyProperties(query, sumQuery);
//        model.put("remainingQuota", accountService.sumRemainingQuotaByQuery(sumQuery));
    }
    
    @RequestMapping("/getAccount")
    public void getAccount(@ModelAttribute("query") AccountQuery query,String callBack,ModelMap model){
        accountService.selectByPage(query);
        model.put("callBack", callBack);
    }

    @RequestMapping("/list/export")
    public void exportAccountList(AccountQuery query, HttpServletResponse response){
        query.setPageSize(30000);
        accountService.selectByPage(query);

        List<AccountVo> exportResult = new ArrayList<AccountVo>();
        for(Account item : query.getData()){
            exportResult.add(new AccountVo(item));
        }
        String[] columnNames = { "交易账号","姓名","创建时间"};
        String fileName = "账户列表";
        ExportExcelWrapper<AccountVo> util = new ExportExcelWrapper<AccountVo>();
        util.exportExcel(fileName, fileName, columnNames, exportResult, response, ExportExcelUtil.EXCEl_FILE_2007);

    }
}
